public class ThreadSync extends Thread {
	 String val = null; ;

	public void run(String arg[]) {
		
		System.out.println("New Thread");
			int i=0;	

			this.setVal("Dodajemy "+i );
			
			String result;
			result = this.getVal();
			System.out.println("czytam " + result);
		//}
	}
	public static void main(String[] args) {
		ThreadSync server = new ThreadSync();
		server.start();
		System.out.println("New Thread");
		int i=0;	
	for (i=1; i<6; i++) {

		(new ThreadSync()).start();
		//server.setVal("Dodajemy "+i );
		System.out.println("Dodany" + i);
	//	return;
	}
	
	for (i=2;i<7;i++) {

		new ThreadSync().start();
		String result = null;
		result = server.getVal();
		System.out.println("czytam " + result);
	}
		
		
		
	}
	public synchronized String getVal() {
		while(val == null){
			try {
				this.wait();
			}
			catch (InterruptedException e){
				
			}
		}
		System.out.println("wyjmuje" + val);
		val = null;
		this.notify();
		return val;
		
	}
	
	public synchronized void setVal(String val) {
		while(val != null){
			try {
				this.wait();
			}
			catch (InterruptedException e){
				
			}
		}
		this.val = val;
		System.out.println("Dodany  " + val);
		this.notify();
	}
	
	
}

